package com;

import org.apache.fop.apps.*;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;

/**
 * Created by tpeng on 2016/4/18.
 */
public class FOPBoot {

    public static void main(String[] args) throws Exception{
        long a = System.currentTimeMillis();

        String path = "D:\\FO_Space\\fop-2.1\\examples\\fo\\fo-reference\\";
        String xslfname = "xml2pdf.xsl";
        String xmlfname = "xslfoRef.xml";

        File xmlFile = new File(path + xmlfname);
        File xslFile = new File(path + xslfname);
        File pdfFile = new File(path + xmlfname+".pdf");

        final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
        FOUserAgent userAgent = fopFactory.newFOUserAgent();

        OutputStream out = new FileOutputStream(pdfFile);
        out = new BufferedOutputStream(out);

        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslFile));

        //transformer.setParameter("versionParam", "1.0");

        Source src = new StreamSource(xmlFile);
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(src, res);
        out.close();
        long b = System.currentTimeMillis();
        System.out.println((b-a)+"ms");
    }

    public void convertFO2PDF(File fo, File pdf) throws IOException, FOPException {

        OutputStream out = null;

        try {
            FopFactory fopFactory = FopFactory.newInstance(new File("D:\\FO_Space\\fop.xconf"));

            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            out = new FileOutputStream(pdf);
            out = new BufferedOutputStream(out);

            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();

            Source src = new StreamSource(fo);

            Result res = new SAXResult(fop.getDefaultHandler());

            transformer.transform(src, res);

            FormattingResults foResults = fop.getResults();
            java.util.List pageSequences = foResults.getPageSequences();
            for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
                PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
                System.out.println("PageSequence "
                        + (String.valueOf(pageSequenceResults.getID()).length() > 0
                        ? pageSequenceResults.getID() : "<no id>")
                        + " generated " + pageSequenceResults.getPageCount() + " pages.");
            }
            System.out.println("Generated " + foResults.getPageCount() + " pages in total.");

        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        } finally {
            out.close();
        }
    }

}
